module PopUpManager {
	export var darkSprite:egret.Sprite;

	/**
	 * 添加面板方法
	 * panel			需要显示的UI组件
	 * dark				背景是否变黑
	 * popUpWidth		指定弹窗宽度，定位使用
	 * popUpHeight		指定弹窗高度，定位使用
	 * effectType		0:没有动画，1：从中间轻微弹出，2：从中间猛烈弹出，3：从左向右，4：从右向左，5：从上到下，6：从下到上
	 */
	export function addPopUp(panel, dark: boolean = false, popUpWidth: number = 0, popUpHeight: number = 0, effectType: number = 0, isAlert: boolean = false)
	{
		//判断是否包含panel
		if(GameLayerManager.gameLayer().panelLayer.contains(panel))
		{
			return;
		}

		panel.scaleX = 1;
		panel.scaleY = 1;
		panel.x = 0;
		panel.y = 0;
		panel.alpha = 1;


		if(dark){
			this.darkSprite = new egret.Sprite();
			this.darkSprite.graphics.clear();
			this.darkSprite.graphics.beginFill(0x000000, 0.3);
			this.darkSprite.graphics.drawRect(0, 0, GameConfig.curWidth(), GameConfig.curHeight());
			this.darkSprite.graphics.endFill();
			this.darkSprite.width = GameConfig.curWidth();
			this.darkSprite.height = GameConfig.curHeight();

			if(!GameLayerManager.gameLayer().panelLayer.contains(this.darkSprite)){
				GameLayerManager.gameLayer().panelLayer.addChild(this.darkSprite);
			}

			this.darkSprite.touchEnabled = true;

			egret.Tween.get(this.darkSprite).to({alpha:1},150);
			this.darkSprite.visibal = true;
		}

		GameLayerManager.gameLayer().panelLayer.addChild(panel);
		GameConfig.curPanel = panel;

		if(popUpWidth !=0 ){
			panel.x = GameConfig.curWidth()/2 - popUpWidth/2;
			panel.y = GameConfig.curHeight()/2 - popUpHeight/2;
		}else{
			popUpWidth = panel.width;
			popUpHeight = panel.height;
		}


		//以下是弹窗动画
		var leftX:number = GameConfig.curWidth()/2 - popUpWidth/2;
		var upY:number = GameConfig.curHeight()/2 - popUpHeight/2;

		switch(effectType){
			case 0:
				break;
			case 1:
				panel.alpha = 0;
				panel.scaleX = 0.5;
				panel.scaleY = 0.5;
				panel.x = panel.x + popUpWidth/4;
				panel.y = panel.y + popUpHeight/4;
				egret.Tween.get(panel).to({alpha:1, scaleX:1, scaleY:1, x:(panel.x - popUpWidth/4), y:(panel.y - popUpHeight/4)},300, egret.Ease.backOut);
				break;
			case 2:
				panel.alpha = 0;
				panel.scaleX = 0.5;
				panel.scaleY = 0.5;
				panel.x = panel.x + popUpWidth/4;
				panel.y = panel.y + popUpHeight/4;
				egret.Tween.get(panel).to({alpha:1, scaleX:1, scaleY:1, x:(panel.x - popUpWidth/4), y:(panel.y - popUpHeight/4)},600, egret.Ease.elasticOut);
				break;
			case 3:
				panel.x = - popUpWidth;
				if(isAlert){
					egret.Tween.get(panel).to({x:leftX}, 500, egret.Ease.cubicOut);
				}else{
					egret.Tween.get(panel).to({x:0}, 500, egret.Ease.cubicOut);
				}
				break;
			case 4:
				panel.x = popUpWidth;
				if(isAlert){
					egret.Tween.get(panel).to({x:leftX}, 500, egret.Ease.cubicOut);
				}else{
					egret.Tween.get(panel).to({x:0}, 500, egret.Ease.cubicOut);
				}
				break;
			case 5:
				panel.y = - popUpHeight;
				if(isAlert){
					egret.Tween.get(panel).to({y:upY}, 500, egret.Ease.cubicOut);
				}else{
					egret.Tween.get(panel).to({y:0}, 500, egret.Ease.cubicOut);
				}
				break;
			case 6:
				panel.y = popUpHeight;
				if(isAlert){
					egret.Tween.get(panel).to({y:upY}, 500, egret.Ease.cubicOut);
				}else{
					egret.Tween.get(panel).to({y:0}, 500, egret.Ease.cubicOut);
				}
				break;
			default:
				break;
		}

	}



	/**
	 * 移除面板方法
	 * pannel			面板
	 * effectType		0:没有动画，1：从中间轻微弹出，2：从中间猛烈弹出，3：从左向右，4：从右向左，5：从上到下，6：从下到上
	 */
	export function removePopUp(panel, effectType:number = 0):void
	{
		var onComplete:Function = function(){
			if(GameLayerManager.gameLayer().panelLayer.contains(this.darkSprite)){
				GameLayerManager.gameLayer().panelLayer.removeChild(this.darkSprite);
			}
		};

		if(this.darkSprite){
			egret.Tween.get(this.darkSprite).to({alpha:0}, 100).call(onComplete,this);
		}

		switch(effectType){
			case 0:
				break;
			case 1:
				egret.Tween.get(panel).to({alpha:0, scaleX:0, scaleY:0, x:(panel.x + panel.width/2), y:(panel.y + panel.height/2)},300);
				break;
			case 2:
				break;
			case 3:
				egret.Tween.get(panel).to({x:panel.width}, 500, egret.Ease.cubicOut);
				break;
			case 4:
				egret.Tween.get(panel).to({x:-panel.width}, 500, egret.Ease.cubicOut);
				break;
			case 5:
				egret.Tween.get(panel).to({y:panel.height}, 500, egret.Ease.cubicOut);
				break;
			case 6:
				egret.Tween.get(panel).to({y:-panel.height}, 500, egret.Ease.cubicOut);
				break;
			default:
				break;
		}

		var waitTime:number = 500;
		if(effectType == 0){
			waitTime = 0;
		}
		egret.setTimeout(function(){
			if(GameLayerManager.gameLayer().panelLayer.contains(panel))
			{
				GameLayerManager.gameLayer().panelLayer.removeChild(panel);
			}
		},this,waitTime);
	}
}